﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using MilitaryPluginInterface;

namespace MilitaryPluginInterface
{
    public class NormalAIBrain : AIBrain
    {
        public NormalAIBrain(Military brainOwner)
            : base(brainOwner)
        {
            
        }

        public override Military findEnemyForAttack()
        {
            return findNearestEnemyForAttack();
        }

        public override Military findEnemyForAttack(List<Military> militaries, Military hero)
        {
            return findNearestEnemyForAttack(militaries, hero);
        }

        public override Factory findFactoryForAttack()
        {
            return findNearestFactoryForAttack();
        }

        private Factory findNearestFactoryForAttack()
        {
            Factory enemyFactory = null;
            float distance = 9999999999;
            foreach (Team team in _teams)
            {
                if (team.TeamName != _brainOwner.TeamOwner.TeamName)
                {
                    foreach (Factory factory in team.Factories)
                    {
                        float newDistance = Vector2.Distance(factory.Position, _brainOwner.Position);
                        if (newDistance < distance)
                        {
                            if (!factory.isDestroyed())
                            {
                                distance = newDistance;
                                enemyFactory = factory;
                            }
                        }
                    }
                }
            }
            return enemyFactory;
        }

        private Military findNearestEnemyForAttack()
        {
            foreach (Team team in _teams)
            {
                if (team.TeamName != _brainOwner.TeamOwner.TeamName)
                {
                    return findNearestEnemyForAttack(team.Infantries, team.Hero);
                }
            }
            return null;
        }

        private Military findNearestEnemyForAttack(List<Military> militaries, Military hero)
        {
            Military enemy = null;
            float distance = 9999999999;
            foreach (Military military in militaries)
            {
                float newDistance = Vector2.Distance(military.Position, _brainOwner.Position);
                if (newDistance < distance)
                {
                    if (!military.isDead())
                    {
                        distance = newDistance;
                        enemy = military;
                    }
                }
            }
            if(distance > Vector2.Distance(hero.Position, _brainOwner.Position) && !hero.isDead())
                enemy = hero;
            return enemy;
        }
    }
}
